package com.singfan.core.dao.database.impl;

import com.singfan.common.db.DaoBaseGeneric;
import com.singfan.common.db.DbContext;
import com.singfan.common.db.config.Table;
import com.singfan.core.dao.database.IBarberTokenDao;
import com.singfan.core.dao.database.model.BarberToken;
import org.springframework.stereotype.Repository;

import java.util.Date;

/**
 *
 * Author: hansonghe
 * Date: 2015-08-19 12:12
 * Desc:
 */
@Repository
public class BarberTokenDaoImpl extends DaoBaseGeneric<BarberToken> implements IBarberTokenDao {
    @Override
    protected String getTableName(Object hashKey) {
        return tableNameBase;
    }

    @Override
    protected String getDatabaseId(Object hashKey) {
        return "app_1";
    }

    @Override
    public void insertOrUpdateToken(long barberId, String token, int cityId, int status) {
        BarberToken barberToken = new BarberToken();
        barberToken.setBarberId(barberId);
        barberToken.setAccessToken(token);
        barberToken.setStatus(status);
        barberToken.setCityId(cityId);
        barberToken.setTokenCreateTime(new Date());

        replace(barberId, barberToken);
    }

    @Override
    public BarberToken findBarberToken(String token) {
        String sql = "SELECT * FROM " + getTableName(null) + " WHERE access_token=?";
        return selectOne(null, sql, token);
    }

    @Override
    public void delete(long barberId) {
        deleteByPK(null, barberId);
    }
}
